<?php

namespace app\dao;

use app\model\TrainingLogModel;
use think\facade\Log;

class TrainingLogDao extends BaseDao
{
    private $query;

    protected function setModel()
    {
        return TrainingLogModel::class;
    }

    public function __construct()
    {
        $this->query = new TrainingLogModel();
    }

    // 根据任务 ID 获取训练日志
    public static function getLogsByTaskId($taskId)
    {
        try {
            return TrainingLogModel::where('task_id', $taskId)->select();
        } catch (\Exception $e) {
            Log::error("根据任务ID获取训练日志失败: " . $e->getMessage());
            return [];
        }
    }

    // 根据成员 ID 获取训练日志
    public static function getLogsByMemberId($memberId)
    {
        try {
            return TrainingLogModel::where('member_id', $memberId)->select();
        } catch (\Exception $e) {
            Log::error("根据成员ID获取训练日志失败: " . $e->getMessage());
            return [];
        }
    }

    // 创建训练日志
    public static function createLog($memberId, $taskId, $logContent)
    {
        try {
            return TrainingLogModel::create([
                'member_id' => $memberId,
                'task_id' => $taskId,
                'log_content' => $logContent
            ]);
        } catch (\Exception $e) {
            Log::error("创建训练日志失败: " . $e->getMessage());
            return null;
        }
    }

    // 更新训练日志
    public static function updateLog($logId, array $data)
    {
        try {
            return TrainingLogModel::where('id', $logId)->update($data);
        } catch (\Exception $e) {
            Log::error("更新训练日志失败: " . $e->getMessage());
            return false;
        }
    }
}
